package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.RoadLineDetailsActivity;
import com.runen.infinitemovement.adapter.StarRatingAdapter;
import com.runen.infinitemovement.model.entity.PathSectionListEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;
//import com.umeng.socialize.ShareAction;
//import com.umeng.socialize.UMShareListener;
//import com.umeng.socialize.bean.SHARE_MEDIA;
//import com.umeng.socialize.media.UMImage;
//import com.umeng.socialize.media.UMWeb;

public class RoadIntroductionPoppupWindow2 extends PopupWindow {
    private ImageView iv_dismiss;
    private TextView tv_last_road_name, tv_introduction, tv_next_road_name, tv_see_details,tv_prize1;
    private LinearLayout ll_bottom_text;
    private Context context;
    private View layout;
    private String name;
    private String introduction;
    private RoundedCornersImage rci_bg;
    private RecyclerView rv_star;
    private int flag;
    private PathSectionListEntity pathSectionListEntity;
    private StarRatingAdapter starRatingAdapter;

    public OnClickListenr getOnClickListenr() {
        return onClickListenr;
    }

    public void setOnClickListenr(OnClickListenr onClickListenr) {
        this.onClickListenr = onClickListenr;
    }

    private OnClickListenr onClickListenr;

    public static RoadIntroductionPoppupWindow2 getInstance(Context context, PathSectionListEntity pathSectionListEntity) {
        return new RoadIntroductionPoppupWindow2(context, pathSectionListEntity);
    }

    private RoadIntroductionPoppupWindow2(Context context, PathSectionListEntity pathSectionListEntity) {
        super(context);
        this.context = context;
        this.pathSectionListEntity = pathSectionListEntity;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }


    private void initView() {
        layout = View.inflate(context, R.layout.popup_road_introduction2, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_last_road_name = layout.findViewById(R.id.tv_last_road_name);
        tv_see_details = layout.findViewById(R.id.tv_see_details);
        tv_next_road_name = layout.findViewById(R.id.tv_next_road_name);
        rci_bg = layout.findViewById(R.id.rci_bg);
        rv_star = layout.findViewById(R.id.rv_star);
        tv_prize1=layout.findViewById(R.id.tv_prize1);
        tv_prize1.setText(pathSectionListEntity.prize);
        if (pathSectionListEntity.bannerType==1){
            GlideUtils.loadImage(context,pathSectionListEntity.linkAddr + "?x-oss-process=video/snapshot,t_0", rci_bg);
        }else {
            GlideUtils.loadImage(context, pathSectionListEntity.linkAddr, rci_bg);
        }
        tv_last_road_name.setText(pathSectionListEntity.startPoint);
        tv_next_road_name.setText(pathSectionListEntity.endPoint);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_star.setLayoutManager(layoutManager);
        starRatingAdapter = new StarRatingAdapter(context);
        rv_star.setAdapter(starRatingAdapter);
        List<String> starList = new ArrayList<>();
        for (int j = 0; j < pathSectionListEntity.difficulty; j++) {
            starList.add("ss");
        }
        starRatingAdapter.setNewData(starList);
    }

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_see_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pathSectionListEntity != null) {
                    Intent intent = new Intent(context, RoadLineDetailsActivity.class);
                    intent.putExtra("pathId", pathSectionListEntity.sectionId);
                    context.startActivity(intent);
                    dismiss();
                }
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.4f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnClickListenr {
        void onCancelClick();

        void onSureClick();
    }
}
